package com.tengju.support.infrastructure.repository.message;

import com.google.common.collect.Lists;
import com.tengju.support.domain.model.message.Message;
import com.tengju.support.domain.model.message.MessageRepository;
import com.tengju.support.domain.model.message.*;
import com.tengju.support.domain.model.user.UserId;
import com.tengju.support.infrastructure.persistence.mybatis.dataobject.AppMessageCentreDetailDO;
import com.tengju.support.infrastructure.persistence.mybatis.dataobject.AppMessageCentreUnreadCountDO;
import com.tengju.support.infrastructure.persistence.mybatis.dataobject.SupportMessageDO;
import com.tengju.support.infrastructure.persistence.mybatis.dataobject.SupportMessageContentDO;
import com.tengju.support.infrastructure.persistence.mybatis.mapper.tengju.custom.SupportMessageMapper;
import com.tengju.support.infrastructure.repository.message.convert.MessageBatchRelationConvert;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


@Repository
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class MessageRepositoryImpl implements MessageRepository {

    private final SupportMessageMapper supportMessageBatchRelationMapper;

    @Override
    public void batchSave(List<Message> messageBatchRelations) {
        List<SupportMessageDO> supportMessageDOList =
                messageBatchRelations.stream().map(MessageBatchRelationConvert::convertToDO)
                        .collect(Collectors.toList());
        supportMessageBatchRelationMapper.batchInsert(supportMessageDOList);
    }

    @Override
    public void batchUpdateSendStatusByBatchId(BatchId batchId, SendStatus afterStatus) {
        supportMessageBatchRelationMapper.batchUpdateSendStatusByBatchId(batchId.toString(),afterStatus.getVal());
    }

    @Override
    public void updateSendStatusByUserIdMessageId(UserId userId, TaskId messageId, SendStatus sendStatus) {
        supportMessageBatchRelationMapper.updateSendStatusByUserIdMessageId(userId.getValue(),messageId.getValue(),sendStatus.getVal());
    }

    @Override
    public List<Message> getUnSendRecordByMessageId(TaskId messageId) {
        List<SupportMessageDO> supportMessageDOList =
                supportMessageBatchRelationMapper.queryUnSendRecordByMessageId(messageId.getValue(), SendStatus.SENDING.getVal());
        return supportMessageDOList.stream()
                .map(MessageBatchRelationConvert::convertToModel).collect(Collectors.toList());
    }

    /**
     * 获取所有未读消息的数量
     */
    @Override
    public Long getAllUnreadTotal(Long userId, List<Integer> messageTypeList,
                                  Integer readStatus,Integer sendStatus) {
        return supportMessageBatchRelationMapper.getAllUnreadTotal(userId,messageTypeList,readStatus,sendStatus);
    }

    /**
     * 获取对应消息类型未读消息数量
     */
    @Override
    public List<AppMessageCentreUnreadCount> getMessageTypeUnreadTotal(Long userId,Integer readStatus,Integer sendStatus) {
        List<AppMessageCentreUnreadCountDO> appMessageCentreUnreadCountDO =
                supportMessageBatchRelationMapper.getMessageTypeUnreadTotal(userId,readStatus,sendStatus);
        return MessageBatchRelationConvert.toAppMessageCentreUnreadCountModelList(appMessageCentreUnreadCountDO);
    }

    /**
     * 消息中心-首页
     */
    @Override
    public AppMessageCentreDetail getMyMessageTypeDetail(Long userId, Integer messageType,Integer sendStatus) {
        AppMessageCentreDetailDO detailDO =
                supportMessageBatchRelationMapper.getMyMessageTypeDetail(userId,messageType,sendStatus);
        return MessageBatchRelationConvert.toAppMessageCentreDetailModel(detailDO);
    }

    /***
     * 消息中心-列表页
     */
    @Override
    public List<Message> queryMyMessageByPage(Long userId, Integer messageType,Integer sendStatus, int offset, int limit) {
        List<SupportMessageDO> supportMessageDOList = supportMessageBatchRelationMapper.queryMyMessageByPage(userId,
                messageType,sendStatus,offset,limit);
        if (CollectionUtils.isEmpty(supportMessageDOList)) {
            return null;
        }
        return supportMessageDOList.stream()
                .map(MessageBatchRelationConvert::convertToModel)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }

    /**
     * 消息中心-列表页消息数量
     */
    @Override
    public Integer getMyMessageListTotal(Long userId, Integer messageType,Integer sendStatus) {
        Integer total = supportMessageBatchRelationMapper.getMyMessageListTotal(userId,messageType,sendStatus);
        return total == null ? 0 : total;
    }

    /**
     * 消息未读更新未已读状态
     */
    @Override
    public void updateReadStatus(Long userId, Integer messageType,Integer readStatus, Integer sendStatus) {
        supportMessageBatchRelationMapper.updateReadStatus(userId,messageType,readStatus,sendStatus);
    }

    @Override
    public int countUnSendByMessageId(TaskId messageId) {
        return supportMessageBatchRelationMapper
                .countBySendStatusAndMessageId(List.of(SendStatus.NOT_SEND.getVal(),SendStatus.SENDING.getVal()),messageId.getValue());
    }
}
